package com.itheima.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.wms.common.service.CrudServiceImpl;
import com.itheima.wms.dao.OwnerLocationMapper;
import com.itheima.wms.entity.LocationEntity;
import com.itheima.wms.entity.OwnerLocationEntity;
import com.itheima.wms.service.LocationService;
import com.itheima.wms.service.OwnerLocationService;
import com.itheima.wms.vo.OwnerLocationDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * <p>
 * 业务实现类
 * 货主-库位关联表
 * </p>
 */
@Slf4j
@Service
public class OwnerLocationServiceImpl extends CrudServiceImpl<OwnerLocationMapper, OwnerLocationEntity> implements OwnerLocationService {

    @Autowired
    private LocationService locationService;

    @Override
    public IPage<OwnerLocationDetailVO> pageDetail(Page<OwnerLocationDetailVO> page, Map data) {
        IPage<OwnerLocationDetailVO> iPage = this.baseMapper.selectOwnerLocationDetail(page, data);
        return iPage;
    }

    @Override
    @Transactional
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        List<OwnerLocationEntity> ownerLocationEntities = this.listByIds(idList);
        Set<Long> locationIds = ownerLocationEntities.stream().map(item -> item.getLocationId()).collect(Collectors.toSet());
        LambdaUpdateWrapper<LocationEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(LocationEntity::getOwnerId, null);
        wrapper.in(LocationEntity::getId, locationIds);
        locationService.update(wrapper);
        return super.removeByIds(idList);
    }

}

